package com.cskaoyan.controller;

import com.cskaoyan.bean.BaseRespVo;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;

/**
 * @author stone
 * @date 2021/12/31 16:40
 */
@RestController
public class ArticleController {

    //localhost:8080/article/details
    //提供占位符，一个占位符在article之前，一个占位符在details之后；占位符写{xxx}，xxx是占位符的名称
    //使用的是@PathVariable注解的value属性来获得对应占位符的值
    @RequestMapping("{aaa}/article/details/{bbb}")
    public BaseRespVo articleDetails(@PathVariable("aaa")String username,
                                     @PathVariable("bbb")Integer id) {
        return BaseRespVo.ok(username + ":" + id);
    }

    //@RequestParam注解的value属性和形参名对应起来，形参就可以接收到对应的值
    // 同时会限定要携带对应的请求参数
    //而这个过程我们直接让形参名和请求参数名一致就可以做到
    //localhost:8080/user/login?username=songge&password=niupi
    @RequestMapping("user/login")
    public BaseRespVo login(@RequestParam("username") String usernamex,
                            @RequestParam("password") String passwordy) {
        return BaseRespVo.ok();
    }

    //可以通过String或String[]来接收对应的值
    //如果以数组来接收，其实就是将字符串根据逗号作为分隔符转换数组
    @RequestMapping("header")
    public BaseRespVo header(@RequestHeader("Accept")String[] accept,
                             @RequestHeader("Host") String host) {
        //String usernamesString = "songge,ligenli,xuejia";
        //String[] split = usernamesString.split(",");
        return BaseRespVo.ok();
    }

    @RequestMapping("cookie/value")
    public BaseRespVo cookieValue(@CookieValue("songge")String value) {
        return BaseRespVo.ok(value);
    }

    @RequestMapping("session/put/{username}")
    public BaseRespVo putValue(@PathVariable("username")String username, HttpSession session) {
        session.setAttribute("username", username);
        return BaseRespVo.ok();
    }

    //放入的时候放入的是什么类型，接收的时候就是什么类型
    //简化的是session.getAttribute
    @RequestMapping("session/get")
    public BaseRespVo sessionGet(@SessionAttribute("username")String username) {
        return BaseRespVo.ok(username);
    }
}
